// screens/splash_screen.dart
import 'package:flutter/material.dart';
// import 'package:provider/provider.dart';
// import 'package:hm_pda_app/router/auth_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();
    _initializeApp();
  }

  Future<void> _initializeApp() async {
   // final auth = Provider.of<AuthProvider>(context, listen: false);
   final prefs = await SharedPreferences.getInstance();
   var isLoggedIn = prefs.getBool('isLoggedIn') ?? false;
   var token = prefs.getString('token') ?? '';
    // 模拟应用初始化过程
    await Future.delayed(const Duration(milliseconds: 1000));

    // 尝试自动登录
   //await auth.autoLogin();

    // 延迟一下让用户看到启动页
    await Future.delayed(const Duration(milliseconds: 500));
    print('mounted---$mounted,$token');
     print('获取token---$token');
    if (mounted) {
      if (isLoggedIn) {
        // 自动登录成功，跳转到首页
        Navigator.of(context).pushReplacementNamed('/Home');
      } else {
        // 需要手动登录
        Navigator.of(context).pushReplacementNamed('/Login');
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const FlutterLogo(size: 100),
            const SizedBox(height: 20),
            Text(
              '海麦PDA',
              style: TextStyle(
                color: Colors.white,
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 20),
            const CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
            ),
          ],
        ),
      ),
    );
  }
}
